//
//  TableViewController.swift
//  LunchDecider
//
//  Created by 王阳 on 2017/5/5.
//  Copyright © 2017年 WayneStudio. All rights reserved.
//

import Foundation
import UIKit

class TableViewController: UITableViewController {
    
    var dataArray = [(id: Int, name: String)]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "饭店编辑"
        
        let item = UIBarButtonItem.init(barButtonSystemItem: .add, target: self, action: #selector(addButtonClick))
        self.navigationItem.rightBarButtonItem = item
        
        self.tableView.tableFooterView = UIView.init()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.dataArray = SQLiteManager.sharedInstance.selectAll()
    }
    
    //MARK: - ClickActions
    
    func addButtonClick() {
        modifyRestaurantName(index: nil)
    }
    
    //MARK: - DataModify
    
    func modifyRestaurantName(index: Int?) {
        let alert = UIAlertController.init(title: "饭店编辑", message: "请输入饭店名称", preferredStyle: .alert)
        alert.addTextField { (textField) in
            if let currentIndex = index {
                textField.placeholder = self.dataArray[currentIndex].name    //点击Cell,存在Index,则显示当前饭店名称
            }
            else {
                textField.placeholder = "请输入饭店名称"   //点击"+",不存在Index,则显示默认文字
            }
        }
        alert.addAction(UIAlertAction.init(title: "确定", style: .default, handler: { (action) in
            let name = (alert.textFields?.first?.text)!
            if !name.isEmpty {
                if let currentIndex = index {
                    SQLiteManager.sharedInstance.update(id: self.dataArray[currentIndex].id, name: name)    //点击Cell,存在Index,则修改数组中的文字
                }
                else {
                    SQLiteManager.sharedInstance.insert(name: name) //点击"+",不存在Index,则追加文字
                }
                self.dataArray = SQLiteManager.sharedInstance.selectAll()
                self.tableView.reloadData()
            }
            alert.dismiss(animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
        }))
        self.present(alert, animated: true, completion: nil)
    }
    
    //MARK: - TableViewDataSource
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")!
        cell.selectionStyle = .none
        cell.textLabel?.text = self.dataArray[indexPath.row].name
        return cell
    }
    
    //Mark: - TableViewDelegate
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        modifyRestaurantName(index: indexPath.row)
    }
    
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return .delete
    }
    
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            SQLiteManager.sharedInstance.delete(id: self.dataArray[indexPath.row].id)
            self.dataArray = SQLiteManager.sharedInstance.selectAll()
            self.tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }

}
